﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.FlowStep
{
    public partial class Examination : PageBase
    {
        private DataSet GridDataSource
        {
            get
            {
                return partyjoinexamination.GridDataSource(this.AppllicationID);
            }
        }

        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }
        private void BindData()
        {
            //新增
            string addThinkUrl = "ExaminationEdit.aspx?StandID=" + this.CurrentStandID;
            this.btnAddThink.OnClientClick = this.WAdd.GetShowReference(addThinkUrl, "新增证明材料");


        }

        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            this.GridExamination.DataSource = this.GridDataSource;
            this.GridExamination.DataBind();
            this.GridExamination.RecordCount = this.GridDataSource.Tables[0].Rows.Count;
            PartyJoinLinkExample oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);

            PartyJoinProcess ps = DB.PartyJoinProcesss.Find(this.CurrentStepID);
            this.lbWorkGuide.Text = Convert.ToString(ps.WorkPoint) ?? "";

            if (oModel.Status == 0)
            {
                this.ddlPoliticalReview.SelectedValue = "1";
            }
            //页面只读显示
            if (this.ReadOnly.Trim() == "1")
            {
                this.btnSubmit.Visible = false;
                this.btnSaveClose.Visible = false;
                //if (IsJoinPartyAdmin && this.CurrentStepID != ProcesUtil.JoinStep9)
                //{
                //    this.btnSaveClose.Visible = true;
                //}
            }
        }



        private void SaveData(bool isSubmit = false)
        {
            if (isSubmit)
            {
                string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过
                bool bResult = false;
                PartyJoinLinkExample oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                oModel.HandleUserID = this.CurrentUser.ID;
                oModel.HandleUserName = this.CurrentUser.UserName;
                oModel.HandleTime = DateTime.Now;
                oModel.Status = (int)Enum_FlowStanceStatus.通过;
                bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);

                if (bResult)
                {
                    Alert.Show("提交成功", String.Empty, ActiveWindow.GetHideReference());
                }
                else
                {
                    if (Message != "")
                    {
                        Alert.Show(Message, String.Empty, ActiveWindow.GetHideReference());
                    }
                }
            }
            else
            {
                Alert.Show("保存成功", String.Empty, ActiveWindow.GetHideReference());
            }
            
        }
        #endregion

        #region 按钮
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.GridExamination.RecordCount > 0)
            {
                this.SaveData(true);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                Alert.Show("请至少添加一条证明材料！", "提示");
            }
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            if (this.GridExamination.RecordCount > 0)
            {
                this.SaveData();
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                Alert.Show("请至少添加一条证明材料！", "提示");
            }
        }
        #endregion

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridExamination_RowCommand(object sender, GridCommandEventArgs e)
        {
            object[] values = GridExamination.DataKeys[e.RowIndex];
            string ID = Convert.ToString(values[0]);
            if (e.CommandName == "Delete")
            {
                var del = DB.partyjoinexaminations.FirstOrDefault(t => t.ID == ID);
                DB.partyjoinexaminations.Remove(del);
                DB.SaveChanges();
            }
            LoadData();
        }

        protected void WAdd_Close(object sender, WindowCloseEventArgs e)
        {
            LoadData();
        }
    }
}